package com.hdoit.apollo.web.stock.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.inventory.model.InventoryShow;
import com.hdoit.apollo.logical.inventory.model.InventoryStaff;
import com.hdoit.apollo.logical.inventory.service.IInventoryStaffService;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;

@Controller
@RequestMapping(value = "/api/v1/inventoryStaff/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/inventoryStaff")
public class InventoryStaffApi extends BaseApi{

	@Autowired
	private IInventoryStaffService inventoryStaffService;
	
	private final String saveInventoryStaffApi="saveInventoryStaff";
	
    @RequestMapping(value = saveInventoryStaffApi)
    @ApiOperation(notes = saveInventoryStaffApi, httpMethod = "POST", value = "保存人员盘点信息")
    public @ResponseBody String saveInventoryStaff(InventoryStaff inventoryStaff,String inventoryShowsJson){
    	InventoryStaff inventoryStaffPan = inventoryStaffService.queryModel(inventoryStaff.getInventoryStaffId());
    	if(inventoryStaffPan==null){
    		inventoryStaff.setCreateDate(DateUtils.getCurrent());
    		inventoryStaff.setCreateUserId(getLoginId());
    		inventoryStaff.setCreateUserName(getLoginName());
            inventoryStaff.setDeleteMark(YesOrNoInteger.no);
            inventoryStaff.setModelStatus(ModelStatus.add);
    	}else{
    		inventoryStaff.setModifyUserId(getLoginId());
    		inventoryStaff.setModifyUserName(getLoginName());
    		inventoryStaff.setModifyDate(DateUtils.getCurrent());
    		inventoryStaff.setModelStatus(ModelStatus.update);
    	}
    	
    	@SuppressWarnings("unchecked")
		List<InventoryShow> inventoryStaffShows = (List<InventoryShow>) JsonUtils.readValues(inventoryShowsJson, List.class, InventoryShow.class);
    	
    	ResponseModel rm=new ResponseModel();
    	if(!inventoryStaffService.applyInventoryStaff(inventoryStaff,inventoryStaffShows)){
    		rm.setStatus(ResponseStatus.dataError);
    	}
    	return rm.toJson();
    }

}
